package com.dianxun.exam.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.dianxun.exam.MainApplication;
import com.dianxun.exam.common.AppConfig;
import com.dianxun.exam.common.AppKeyManager;
import com.dianxun.exam.common.base.BaseActivity;

import com.dianxun.exam.R;
import com.dianxun.exam.entity.DaoSession;
import com.dianxun.exam.entity.TikuBean;
import com.dianxun.exam.entity.TikuBeanDao;
import com.dianxun.exam.entity.UserListBean;
import com.dianxun.exam.entity.UserListBeanDao;
import com.dianxun.exam.utils.UpdateInfo;

import java.util.List;

import butterknife.BindView;

/**
 * 登录
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.et_user)
    EditText et_user;
    @BindView(R.id.et_password)
    EditText et_password;
    @BindView(R.id.btn_ret)
    Button btn_ret;
    @BindView(R.id.btn_login)
    Button btn_login;
    @BindView(R.id.tv_download)
    TextView tv_download;
    @BindView(R.id.tv_version)
    TextView tv_version;
    private DaoSession daoSession;
    private TikuBeanDao tikuBeanDao;
    private UserListBeanDao userListBeanDao;

    @Override
    protected int getContentView() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        daoSession = ((MainApplication) getApplication()).getDaoSession();
        userListBeanDao = daoSession.getUserListBeanDao();
        tikuBeanDao = daoSession.getTikuBeanDao();
        tv_version.setText("ver" + UpdateInfo.getVersionName(this));
        //检测是否有版本更新
        new Thread() {
            @Override
            public void run() {
                UpdateInfo.CheckVersion(LoginActivity.this);// 检查更新
            }
        }.start();

        // 测试数据
//        et_user.setText("admin");
//        et_password.setText("123");
//        et_user.setText("gyj215");
//        et_password.setText("123");
    }

    @Override
    protected void initListeners() {
        btn_ret.setOnClickListener(this);
        btn_login.setOnClickListener(this);
        tv_download.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_ret:
                finish();
                break;
            case R.id.tv_download:
                startActivity(new Intent(self, DownloadActivity.class));
                break;
            case R.id.btn_login:
                String user = et_user.getText().toString().trim();
                String password = et_password.getText().toString().trim();
                if (TextUtils.isEmpty(user)) {
                    toast("请输入用户名");
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    toast("请输入密码");
                    return;
                }
                List<TikuBean> tikuBeanList = tikuBeanDao.queryBuilder().list();
                if (tikuBeanList.size() == 0) {
                    toast("没有试卷，请点击考官入口下载");
                    return;
                }
                List<UserListBean> userListBeanList = userListBeanDao.queryBuilder().where(UserListBeanDao.Properties.UserName.eq(user)).list();
                if (userListBeanList == null || userListBeanList.size() == 0) {
                    toast("用户不存在");
                    return;
                }
                userListBeanList = userListBeanDao.queryBuilder().where(UserListBeanDao.Properties.UserName.eq(user),
                        UserListBeanDao.Properties.Password.eq(password)).list();
                if (userListBeanList == null || userListBeanList.size() == 0) {
                    toast("密码错误");
                    return;
                }
                UserListBean userListBean = userListBeanList.get(0);
//                if (answerBeanDao.queryBuilder().where(AnswerBeanDao.Properties.UserId.eq(userListBean.getUserId()),
//                        AnswerBeanDao.Properties.TikuId.eq(tikuBeanList.get(0).getTikuId())).list().size() > 0) {
//                    toast("您已完成考试，无法重考");
//                    return;
//                }
                if (userListBean.getRoleStr().contains("admin")) {
                    jumpActivity(UploadActivity.class);
                } else {
                    userListBeanList = userListBeanDao.queryBuilder().where(UserListBeanDao.Properties.UserName.eq(user),
                            UserListBeanDao.Properties.Password.eq(password)).list();
                    if (userListBeanList == null || userListBeanList.size() == 0) {
                        toast("该用户无法考试");
                        return;
                    }
                    AppConfig.getInstance().setUserId(userListBean.getUserId());
                    AppConfig.getInstance().setUsername(userListBean.getUserName());
                    AppConfig.getInstance().setDisplayName(userListBean.getDisplayName());
                    Bundle b = new Bundle();
                    b.putString(AppKeyManager.SP_DISPLAYNAME, userListBean.getDisplayName());
                    jumpActivity(ExamReadyActivity.class, b);
                }
                et_user.setText("");
                et_password.setText("");
                break;
        }
    }
}
